import java.util.NoSuchElementException;
import java.util.Properties;

import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

/**
 * 
 * Routines to manage node colors and color images.
 *
 */
public class NodeColorUtils {
	
	private Display d;
	private Image[] images = new Image[6];
		
	/**
	 * Routines to manage node colors and color images.
	 * @param d
	 */
	public NodeColorUtils(Display d)
	{
		this.d = d;
	}
		
	/**
	 * @param props - Properties object (MapEditor.properties)
	 * @param propName - key to look up in properties file
	 * @return Image - image located at image path specified by propName. 
	 * @throws NoSuchElementException
	 *
	 */
	public Image loadColorImage(Properties props, String propName) throws NoSuchElementException
	{
		String imagePath = props.getProperty(propName);
		Image image;
		
		if (imagePath == null)
			throw new NoSuchElementException("Element \"" + propName + "\" not found in properties file.");
		
		try
		{
			image = new Image(d, imagePath);
		}
		catch (SWTException e)
		{
			e.printStackTrace();
			return null;
		}
		
		return image;
	}
	
	/**
	 * 
	 * Enumerated type containing color index and string name.
	 *
	 */
	public enum NodeColor { Violet (0, "Violet"), Blue (1, "Blue"), Green (2, "Green"), Yellow (3, "Yellow"), Orange (4, "Orange"), Red (5, "Red"), Error (6, "Error");
	
		private int idx;
		private String name;
		
		NodeColor(int idx, String name)
		{
			this.idx = idx;
			this.name = name;
		}
		
		int getIdx()
		{
			return idx;
		}
		
		String getName()
		{
			return name;
		}
	};

	/**
	 * Given a color name, returns a NodeColor enumerated type.
	 * 
	 * @param colorStr
	 * @return NodeColor
	 */
	public NodeColor colorLookup(String colorStr)
	{
		for (NodeColor nc : NodeColor.values())
		{
			if (nc.getName().equalsIgnoreCase(colorStr))
				return nc;
		}
		
		return NodeColor.Error;
	}
	
	/**
	 * given a color index, returns a NodeColor enumerated type.
	 * 
	 * @param colorInt
	 * @return nodeColor
	 */
	public NodeColor colorLookup(int colorInt)
	{
		for (NodeColor nc : NodeColor.values())
		{
			if (nc.idx == colorInt)
				return nc;
		}
		
		return NodeColor.Error;
	}
	
	/**
	 * Returns the string equivalent of a color index, or null if no match was found.
	 * @param idx
	 * @return String - color name
	 */
	public String getColorString(int idx)
	{
		for (NodeColor nc : NodeColor.values())
		{
			if (nc.getIdx() == idx)
				return nc.getName();
		}
		
		return null;
	}
	
	/**
	 * Stores the specified image and associated it with a color.
	 * 
	 * @param nc - NodeColor enumerated type
	 * @param props - Properties object (MapEditor.properties)
	 * @param key - key to look up in properties file
	 */
	public void setImage(NodeColor nc, Properties props, String key)
	{
		images[nc.getIdx()] = loadColorImage(props, key);
	}
	
	/**
	 * Returns the image for the specified color index.
	 * @param idx
	 * @return Image
	 */
	public Image getImage(int idx)
	{
		return images[idx];
	}
}
